package br.com.fpf.pontoapp.view.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import br.com.fpf.pontoapp.R;
import br.com.fpf.pontoapp.model.dto.Batida;
import br.com.fpf.pontoapp.model.dto.InfoDTO;
import br.com.fpf.pontoapp.ws.impl.Conection;

public class TelaListaBatidas extends Activity {
	private List<Batida> batidas = new ArrayList<Batida>();
	private InfoDTO info;
	private String strNome;
	private ProgressDialog dialog;
	ListView lv;
	TextView nome, ultimasBatidas, saidaSugerida;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		Bundle bundle = getIntent().getExtras();
		String matricula = bundle.getString("matricula");
		String senha = bundle.getString("senha");
		WebConection webConection = new WebConection();
		webConection.execute(matricula, senha);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.tela_lista_batidas);
		dialog = ProgressDialog.show(this, null, "Carregando...");
	}

	private class WebConection extends AsyncTask<String, Void, List<Batida>> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected List<Batida> doInBackground(String... params) {

			info = Conection.ResultBatidas(params[0], params[1]);
			batidas = info.getBatidas();
			strNome = info.getNome();
			return batidas;
		}

		@Override
		protected void onPostExecute(List<Batida> result) {
			super.onPostExecute(result);
			dialog.dismiss();
			List<String> dias = new ArrayList<String>();
			for (Batida batida : result) {
				dias.add(batida.getDia());
			}

			nome = (TextView) findViewById(R.id.text_nome);
			nome.setText(strNome);

			ultimasBatidas = (TextView) findViewById(R.id.text_ultimas_batidas);
			ultimasBatidas.setText(result.get(result.size() - 1).getBatidas());

			saidaSugerida = (TextView) findViewById(R.id.text_saida_sugerida);
			saidaSugerida.setText(result.get(result.size() - 1)
					.getSaidaSugerida());

			lv = (ListView) findViewById(R.id.listView1);
			ArrayAdapter<String> ad = new ArrayAdapter<String>(
					getApplicationContext(),
					android.R.layout.simple_list_item_2, android.R.id.text2,
					dias);
			lv.setAdapter(ad);

			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int posicao, long arg3) {

					Batida batida = batidas.get(posicao);

					Intent intent = new Intent();
					intent.setClass(getApplicationContext(),
							TelaBatidaDia.class);
					intent.putExtra("batidas", batida.getBatidas());
					intent.putExtra("horasTrabalhadas",
							batida.getHorasTrabalhadas());
					intent.putExtra("observacao", batida.getObservacao());
					startActivity(intent);
				}
			});

		}

	}

}
